/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package it.unifi.dsi.blide.monitor;

import it.unifi.dsi.blide.run.imp.BliteLocalEnvProviderService;
import it.unifi.dsi.blitese.localenv.LocalEnvironment;
import it.unifi.dsi.blitese.localenv.LocalInstanceMonitor;
import it.unifi.dsi.blitese.localenv.SingleStepper;
import java.awt.BorderLayout;
import javax.swing.JComponent;
import org.openide.util.Lookup;
import org.openide.util.NbBundle;
import org.openide.util.WeakListeners;
import org.openide.windows.CloneableTopComponent;
import org.openide.windows.TopComponent;
//import org.openide.util.Utilities;

/**
 * Top component which displays something.
 */
final class InstanceTopComponent extends CloneableTopComponent {

    private static InstanceTopComponent instance;
    /** path to the icon used by the component and its open action */
//    static final String ICON_PATH = "SET/PATH/TO/ICON/HERE";
    private static final String PREFERRED_ID = "InstanceTopComponent";
    private JComponent myView;

    public InstanceTopComponent(LocalInstanceMonitor monitor) {
        initComponents();
        setName(NbBundle.getMessage(InstanceTopComponent.class, "CTL_InstanceTopComponent"));
        setToolTipText(NbBundle.getMessage(InstanceTopComponent.class, "HINT_InstanceTopComponent"));
//        setIcon(Utilities.loadImage(ICON_PATH, true));

        MonitorScene scene = new MonitorScene(monitor);

        // we add the scene as listener for step executions
        BliteLocalEnvProviderService envService = Lookup.getDefault().lookup(BliteLocalEnvProviderService.class);
        LocalEnvironment env = envService.getLocalEnvironment();
        SingleStepper stepper = env.getStepper();
        //IMPORTANT USING WEEK LISTENER PREVENT MEMORYLEAK
        stepper.addChangeListener(WeakListeners.change(scene, stepper));

        myView = scene.createView();

        shapePane.setViewportView(myView);
        add(scene.createSatelliteView(), BorderLayout.EAST);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        shapePane = new javax.swing.JScrollPane();

        setLayout(new java.awt.BorderLayout());
        add(shapePane, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane shapePane;
    // End of variables declaration//GEN-END:variables

    /**
     * Gets default instance. Do not use directly: reserved for *.settings files only,
     * i.e. deserialization routines; otherwise you could get a non-deserialized instance.
     * To obtain the singleton instance, use {@link #findInstance}.
     */
//    public static synchronized InstanceTopComponent getDefault() {
//        if (instance == null) {
//            instance = new InstanceTopComponent();
//        }
//        return instance;
//    }

    /**
     * Obtain the InstanceTopComponent instance. Never call {@link #getDefault} directly!
     */
//    public static synchronized InstanceTopComponent findInstance() {
//        TopComponent win = WindowManager.getDefault().findTopComponent(PREFERRED_ID);
//        if (win == null) {
//            Logger.getLogger(InstanceTopComponent.class.getName()).warning(
//                    "Cannot find " + PREFERRED_ID + " component. It will not be located properly in the window system.");
//            return getDefault();
//        }
//        if (win instanceof InstanceTopComponent) {
//            return (InstanceTopComponent) win;
//        }
//        Logger.getLogger(InstanceTopComponent.class.getName()).warning(
//                "There seem to be multiple components with the '" + PREFERRED_ID +
//                "' ID. That is a potential source of errors and unexpected behavior.");
//        return getDefault();
//    }

    @Override
    public int getPersistenceType() {
        return TopComponent.PERSISTENCE_NEVER;
    }

    @Override
    public void componentOpened() {
        // TODO add custom code on component opening
    }

    @Override
    public void componentClosed() {
        // TODO add custom code on component closing
    }

    /** replaces this in object stream */
//    @Override
//    public Object writeReplace() {
//        return new ResolvableHelper();
//    }

    @Override
    protected String preferredID() {
        return PREFERRED_ID;
    }

//    final static class ResolvableHelper implements Serializable {
//
//        private static final long serialVersionUID = 1L;
//
//        public Object readResolve() {
//            return InstanceTopComponent.getDefault();
//        }
//    }
}
